package com.ravi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ravi.common.constant.CheckExceptionEnum;
import com.ravi.common.exception.CheckException;
import com.ravi.mapper.CheckItemMapper;
import com.ravi.model.dto.CheckItemDto;
import com.ravi.model.vo.CheckItemVo;
import com.ravi.service.intf.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Service
public class CheckItemServiceImpl implements CheckItemService {

	@Autowired
	private CheckItemMapper checkItemMapper;


	@Override
	public PageInfo<CheckItemVo> listItemsPage(Integer pageNum, Integer pageSize) {
		pageNum = (null == pageNum) || (pageNum <= 0) ? 1 : pageNum;
		PageHelper.startPage(pageNum, pageSize, "room_type_id,limosis_flag");
		List<CheckItemDto> itemDtos = checkItemMapper.getAllCheckItems();
		List<CheckItemVo> vos = Dto2Vo(itemDtos);
		return new PageInfo<CheckItemVo>(vos);
	}

	/**
	 * 接口废弃 使用分页接口
	 */
	@Override
	public List<CheckItemVo> getAllCheckItemVos() {
		List<CheckItemDto> itemDtos = checkItemMapper.getAllCheckItems();
		return Dto2Vo(itemDtos);
	}

	@Override
	public List<CheckItemVo> getVosByRoomTypeName(String roomTypeName) {
		List<CheckItemDto> dtos = checkItemMapper.getCheckItemsByRoomTypeName(roomTypeName);
		if (dtos.isEmpty()) {
			throw new CheckException(CheckExceptionEnum.SEARCH_FAILED);
		}
		return Dto2Vo(dtos);
	}

	@Override
	public List<CheckItemVo> getVosByItemKeyword(String keyword) {
		List<CheckItemDto> dtos = checkItemMapper.getCheckItemsByItemKeyword(keyword);
		if (dtos.isEmpty()) {
			throw new CheckException(CheckExceptionEnum.SEARCH_FAILED);
		}
		return Dto2Vo(dtos);
	}

	@Override
	public List<CheckItemVo> getVosByItemId(Integer id) {
		List<CheckItemDto> dtos = checkItemMapper.getCheckItemsByItemId(id);
		if (dtos.isEmpty()) {
			throw new CheckException(CheckExceptionEnum.SEARCH_FAILED);
		}
		return Dto2Vo(dtos);
	}

	private List<CheckItemVo> Dto2Vo(List<CheckItemDto> dtos) {
		List<CheckItemVo> ret = new CopyOnWriteArrayList<>();
		for (CheckItemDto dto : dtos) {
			CheckItemVo vo = new CheckItemVo(dto);
			ret.add(vo);
		}
		return ret;
	}

}
